package cn.edu.scnu.service;

import cn.edu.scnu.entity.Favourite;
import cn.edu.scnu.mapper.FavouriteMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavouriteService extends ServiceImpl<FavouriteMapper, Favourite> {
    @Autowired
    private FavouriteMapper favouriteMapper;

    // 判断当前用户是否收藏电影
    public Boolean isFavourite(String phone, Integer movieId) {
        QueryWrapper<Favourite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("movie_id", movieId);
        Favourite favourite = favouriteMapper.selectOne(queryWrapper);
        if (favourite != null)
            return true;  // 已收藏
        else
            return false;  //未收藏
    }

    // 收藏电影
    public void addFavourite(String phone, Integer movieId) {
        Favourite favourite = new Favourite();
        favourite.setPhone(phone);
        favourite.setMovieId(movieId);
        favouriteMapper.insert(favourite);
    }

    // 移除收藏
    public void deleteFavorite(String phone, Integer movieId) {
        QueryWrapper<Favourite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        queryWrapper.eq("movie_id", movieId);
        favouriteMapper.delete(queryWrapper);
    }

    // 展示收藏列表
    public List<Favourite> showFavourite(String phone) {
        QueryWrapper<Favourite> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        return favouriteMapper.selectList(queryWrapper);
    }
}
